Skip to content

feat: esm#4509

Open
florian-lefebvre wants to merge 11 commits intolovell:mainfrom
florian-lefebvre:feat/build-step
Open

feat: esm#4509
florian-lefebvre wants to merge 11 commits intolovell:mainfrom
florian-lefebvre:feat/build-step

Conversation

@florian-lefebvre
Copy link
Copy Markdown

@florian-lefebvre florian-lefebvre commented Mar 18, 2026

Closes #2981

I kept this PR as minimal as I could. It introduces esbuild to be able to compile ESM to CJS to support both:

  • I had to migrate lib to ESM because esbuild doesn't support transforming require() into import
  • I had to write a little esbuild plugin to transform TLAs into require()

Things left to do where I'll need some help:

  • I added a build script for esm/cjs but I'm not sure how it relates to install/build.js
  • I'm not sure how tests are currently setup so unsure how to properly test esm/cjs
  • I left some require() calls in libvips.js because they only seem consumed by binding.gyp

@@ -37,9 +37,7 @@ if (sharp && path.startsWith('@img/sharp-linux-x64') && !sharp._isUsingX64V2())
sharp = null;
}

if (sharp) {
module.exports = sharp;
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Conditional exports are not supported by ESM but it doesn't matter here because importing the module will throw as needed

@lovell
Copy link
Copy Markdown
Owner

lovell commented Mar 20, 2026

This is great, thank you for doing the hard work Florian, and at less of an impact in terms of changed code than I had imagined, merci donc d'avoir fait simple.

For the v0.35.x releases I'd like to maintain compatibility with versions of Node.js where ESM isn't fully available and then I suspect the v0.36.x releases (or maybe v1.0.x if I'm feeling lucky after 13 years) is the place for this to land and wish CommonJS bon voyage.

As an aside, I've been invited to (and accepted) the Astro event in London in a few weeks and spotted your name on the list so I might be able to thank you in person for this.

@florian-lefebvre
Copy link
Copy Markdown
Author

Glad I can help :) Yeah I saw you on the list for the event, looking forward to meeting you! I'll give a talk on fonts

Btw I was thinking about this PR during the weekend and I don't know if the packages under npm can be updated to ESM yet. Maybe worth waiting for 0.36 or 1.0 because altho it's just matter of importing the .node file, not sure if that'd work for a CJS project

@florian-lefebvre
Copy link
Copy Markdown
Author

@lovell can you approve the workflow to see if the tests pass?

@florian-lefebvre
Copy link
Copy Markdown
Author

florian-lefebvre commented Apr 2, 2026

To keep this PR as minimal as possible, I switched from ESM first + CJS compat to CJS first + ESM compat, which required a few more changes. Workflow should run better next time you approve it!

@florian-lefebvre
Copy link
Copy Markdown
Author

Build and tests are now passing locally!

],
"scripts": {
"build": "node install/build.js",
"build:dist": "node scripts/build.mjs",
Copy link
Copy Markdown
Author

@florian-lefebvre florian-lefebvre Apr 5, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to add another script for lib to dist compilation because if I understood correctly, sharp can be built from source by doing npm explore sharp -- npm run build. If the compilation happened in this script, it would fail since mjs files in lib are not shipped to NPM anymore

@florian-lefebvre
Copy link
Copy Markdown
Author

I swear this should work now

@florian-lefebvre florian-lefebvre marked this pull request as ready for review April 6, 2026 12:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add support to ES Modules

2 participants